{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#51CTO课程频道：http://edu.51cto.com/lecturer/index/user_id-12330098.html\n",
    "#优酷频道：http://i.youku.com/sdxxqbf\n",
    "#微信公众号：深度学习与神经网络\n",
    "#Github：https://github.com/Qinbf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import os\n",
    "import tarfile\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finish:  inception-2015-12-05.tgz\n"
     ]
    }
   ],
   "source": [
    "#inception模型下载地址\n",
    "inception_pretrain_model_url = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'\n",
    "\n",
    "#模型存放地址,存放在当前目录下inception_model文件夹下\n",
    "inception_pretrain_model_dir = \"inception_model\"\n",
    "if not os.path.exists(inception_pretrain_model_dir):\n",
    "    os.makedirs(inception_pretrain_model_dir)\n",
    "    \n",
    "#获取文件名，以及文件路径\n",
    "filename = inception_pretrain_model_url.split('/')[-1]\n",
    "filepath = os.path.join(inception_pretrain_model_dir, filename)\n",
    "\n",
    "#下载模型\n",
    "if not os.path.exists(filepath):\n",
    "    print(\"download: \", filename)\n",
    "    r = requests.get(inception_pretrain_model_url, stream=True)\n",
    "    with open(filepath, 'wb') as f:\n",
    "        for chunk in r.iter_content(chunk_size=1024):\n",
    "            if chunk:\n",
    "                f.write(chunk)\n",
    "print(\"finish: \", filename)\n",
    "\n",
    "#解压文件\n",
    "tarfile.open(filepath, 'r:gz').extractall(inception_pretrain_model_dir)\n",
    " \n",
    "#模型结构存放文件\n",
    "log_dir = 'inception_log'\n",
    "if not os.path.exists(log_dir):\n",
    "    os.makedirs(log_dir)\n",
    "\n",
    "#classify_image_graph_def.pb为google训练好的模型\n",
    "inception_graph_def_file = os.path.join(inception_pretrain_model_dir, 'classify_image_graph_def.pb')\n",
    "with tf.Session() as sess:\n",
    "    #创建一个图来存放google训练好的模型\n",
    "    with tf.gfile.FastGFile(inception_graph_def_file, 'rb') as f:\n",
    "        graph_def = tf.GraphDef()\n",
    "        graph_def.ParseFromString(f.read())\n",
    "        tf.import_graph_def(graph_def, name='')\n",
    "    #保存图的结构\n",
    "    writer = tf.summary.FileWriter(log_dir, sess.graph)\n",
    "    writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
